package ltd.sumfsy.grain.service.impl;

import ltd.sumfsy.grain.bean.RelationEquipAndData;
import ltd.sumfsy.grain.bean.example.RelationEquipAndDataExample;
import ltd.sumfsy.grain.mapper.RelationEquipAndDataMapper;
import ltd.sumfsy.grain.service.RelationEquipAndDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备和数据之间的联系服务实现类
 * @author 范颂扬
 * @create 2021-05-13 14:22
 */
@Service
public class RelationEquipAndDataServiceImpl implements RelationEquipAndDataService {

    private final RelationEquipAndDataMapper relationMapper;

    @Autowired
    public RelationEquipAndDataServiceImpl(RelationEquipAndDataMapper relationMapper) {
        this.relationMapper = relationMapper;
    }

    @Override
    public boolean build(RelationEquipAndData relation) {
        int res = relationMapper.insertSelective(relation);
        return res > 0;
    }

    @Override
    public boolean delete(RelationEquipAndData relation) {
        RelationEquipAndDataExample example = new RelationEquipAndDataExample();
        example.createCriteria()
                .andDataIdEqualTo(relation.getDataId()).andEquipIdEqualTo(relation.getEquipId());
        int res = relationMapper.updateByExampleSelective(relation.setDelFlag(1), example);
        return res > 0;
    }

    @Override
    public List<RelationEquipAndData> queryAll() {
        RelationEquipAndDataExample example = new RelationEquipAndDataExample();
        example.createCriteria().andDelFlagEqualTo(0);
        return relationMapper.selectByExample(example);
    }

    @Override
    public List<Integer> queryByEquipId(Integer equipId) {
        RelationEquipAndDataExample example = new RelationEquipAndDataExample();
        example.createCriteria().andDelFlagEqualTo(0)
                .andEquipIdEqualTo(equipId);
        List<RelationEquipAndData> relationEquipAndDataList = relationMapper.selectByExample(example);
        List<Integer> dataIds = new ArrayList<>();
        for (RelationEquipAndData relation : relationEquipAndDataList) {
            dataIds.add(relation.getDataId());
        }
        return dataIds;
    }
}
